package pw_coding

import (
	"fmt"
	"path"
	"sasuke/pw_date"
	"strings"
)

// Cs语言生成器
type CsGener struct{}

// 标识
func (g *CsGener) Language() string {
	return "cs"
}

// 后缀
func (g *CsGener) Suffix() string {
	return ".cs"
}

// 模板标识
func (g *CsGener) TplCode() string {
	return "TEMPLE_CS_CLASS"
}

// 字段类型转化器
func (g *CsGener) DataTypeMap(datatype string, cannull bool) string {
	if v, ok := Mysql2CSMap[datatype]; ok {
		if cannull && datatype != "string" {
			return v + "?"
		} else {
			return v
		}
	} else {
		return datatype
	}
}

// 生成器
func (g *CsGener) Coding(p *CmdParam) {
	switch p.GenType {
	case "d":
		g.CodingFromDb(p)
		break
	case "m":
		g.CodingModule(p)
		break
	case "f":
		g.CodingFile(p)
		break
	default:
		g.CodingFromDb(p)
		break
	}
}
func (g *CsGener) CodingFromDb(p *CmdParam) {
	es := PumpFromMysql(p, g.DataTypeMap)
	f := entities2Module(es, g.Suffix(), g.TplCode(), p.Code)
	f.Create()
}
func (g *CsGener) CodingFile(p *CmdParam) {
	fmt.Println("c# file create ...")
	e := New_LangCS_Class_ByCode(p.Code)
	Gen(e)
}
func (g *CsGener) CodingModule(p *CmdParam) {
	f := CSModuleNew(p.Code)
	f.Create()
}

func CSModuleNew(code string) *Folder {
	rootPath := GetWorkDir()
	// 根包
	m := &Folder{}
	m.Name = code
	m.FPath = rootPath

	// 接口子包
	mif := Folder{}
	mif.Name = "Interfaces"
	mif.FOffsetPath = path.Join(code)
	mif.FPath = path.Join(rootPath, code)

	// 实现子包
	mim := Folder{}
	mim.Name = "Implementations"
	mim.FOffsetPath = path.Join(code)
	mim.FPath = path.Join(rootPath, code)

	// 项目文件
	csproj := File{}
	csproj.Name = code
	csproj.FileSuffix = ".csproj"
	csproj.TplCode = "CS_MODULE_CSPROJ_TEMPLE"
	csproj.FPath = path.Join(rootPath, code)
	csproj.FOffsetPath = code
	// 接口实现样例
	iexample := File{}
	iexample.Name = "IExampler"
	iexample.FileSuffix = ".cs"
	iexample.TplCode = "CS_MODULE_INTERFACE_TEMPLE"
	iexample.FOffsetPath = path.Join(code, "Interfaces")
	iexample.FPath = path.Join(rootPath, iexample.FOffsetPath)
	i_args := &LangCS_Class{}
	i_args.Author = "Ivan Wang"
	i_args.CreatedDate = pw_date.PW_Now_New().NormalFmt()
	i_args.ClassName = "接口"
	i_args.ClassCode = iexample.Name
	i_args.NameSpace = strings.Replace(iexample.FOffsetPath, "/", ".", -1)
	i_args.References = []string{"System"}
	iexample.Args = i_args
	// 样例实现
	defexample := File{}
	defexample.Name = "DefExample"
	defexample.FileSuffix = ".cs"
	defexample.TplCode = "CS_MODULE_IMPL_TEMPLE"
	defexample.FOffsetPath = path.Join(code, "Implementations")
	defexample.FPath = path.Join(rootPath, defexample.FOffsetPath)
	d_args := &LangCS_Class{}
	d_args.Author = "Ivan Wang"
	d_args.CreatedDate = pw_date.PW_Now_New().NormalFmt()
	d_args.ClassName = "实现"
	d_args.ClassCode = defexample.Name
	d_args.NameSpace = strings.Replace(defexample.FOffsetPath, "/", ".", -1)
	d_args.References = []string{"System", i_args.NameSpace}
	defexample.Args = d_args

	// 模块加载器
	madaptor := File{}
	madaptor.Name = "ModuleAdaptor"
	madaptor.FileSuffix = ".cs"
	madaptor.TplCode = "CS_MODULE_ADAPTOR_TEMPLE"
	madaptor.FPath = path.Join(rootPath, code)
	madaptor.FOffsetPath = code
	args := &LangCS_Class{}
	args.Author = "Ivan Wang"
	args.CreatedDate = pw_date.PW_Now_New().NormalFmt()
	args.ClassName = "模块加载器"
	args.ClassCode = madaptor.Name
	args.NameSpace = code
	args.References = []string{"System", "Autofac", i_args.NameSpace, d_args.NameSpace}
	madaptor.Args = args

	mif.Files = []File{iexample}
	mim.Files = []File{defexample}
	folders := []Folder{mif, mim}
	files := []File{csproj, madaptor}
	m.Files = files
	m.Folders = folders
	return m
}
